package com.lpf.xypermission.util;

import android.app.Application;

/**
 * Created by lpf on 2022/6/8 0008
 * Describe:
 */
public class ApplicationUtil {

    private static Application mApplication;

    public static Application getApplication() {
        if (mApplication == null) {
            mApplication = getApplicationByReflect();
        }

        return mApplication;

    }

    private static Application getApplicationByReflect() {

        try {
            Class<?> aClass = Class.forName("android.app.ActivityThread");
            Object currentActivityThread = aClass.getMethod("currentActivityThread").invoke(null);
            Object application = aClass.getMethod("getApplication").invoke(currentActivityThread);

            return (Application) application;

        } catch (Exception e) {
            e.printStackTrace();
        }

        throw new NullPointerException("application 为空");

    }

}
